package com.aia.ilp.web;

import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.aia.ilp.domain.Thumb;
import com.aia.ilp.service.MainService;
import com.opensymphony.xwork2.ActionSupport;

public class CustThumbAction extends ActionSupport {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(CustThumbAction.class);

	private String mail;
	private String guid;
	private MainService mainService;
	/**
	 * Logger for this class
	 */

	private static final long serialVersionUID = 1L;

	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}

		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("image/png");
			// response.setHeader("cache", "no-cache");
			OutputStream os = response.getOutputStream();
			Thumb image = mainService.getThumb(guid);
			if (image != null) {
				byte[] imgBin = image.getTHUMB_BIN();
				os.write(imgBin);
			}
			os.flush();
			os.close();
		} catch (javax.imageio.IIOException iioe) {
			if (logger.isDebugEnabled()) {
				logger.debug("execute()"); //$NON-NLS-1$
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("imageAction error:", e);
			throw new RuntimeException(e);
		}
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return null;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getMail() {
		return mail;
	}

	public void setMainService(MainService mainService) {
		this.mainService = mainService;
	}

	public MainService getMainService() {
		return mainService;
	}

	public void setGuid(String guid) {
		this.guid = guid;
	}

	public String getGuid() {
		return guid;
	}

}
